<template>
  <header class="app-header">
    <div class="header-container">
      <div class="logo">
        <router-link to="/">
          <h1>AI表情包生成器</h1>
        </router-link>
      </div>
      <nav class="main-nav">
        <router-link to="/" class="nav-item">基础生成</router-link>
        <router-link to="/advanced" class="nav-item">高级生成</router-link>
        <router-link to="/batch" class="nav-item">批量生成</router-link>
        <router-link to="/history" class="nav-item">历史记录</router-link>
      </nav>
      <div class="user-actions">
        <template v-if="isLoggedIn">
          <el-dropdown>
            <span class="user-dropdown">
              {{ username }} <el-icon><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
        <template v-else>
          <router-link to="/login" class="login-btn">登录</router-link>
          <router-link to="/register" class="register-btn">注册</router-link>
        </template>
      </div>
    </div>
  </header>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowDown } from '@element-plus/icons-vue'

// 模拟用户登录状态
const isLoggedIn = ref(localStorage.getItem('token') !== null)
const username = ref(localStorage.getItem('username') || '用户')
const router = useRouter()

const logout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('userId')
  localStorage.removeItem('username')
  isLoggedIn.value = false
  ElMessage.success('已退出登录')
  router.push('/login')
}
</script>

<style scoped>
.app-header {
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  max-width: 1200px;
  margin: 0 auto;
  height: 64px;
}

.logo a {
  text-decoration: none;
  color: var(--primary-color);
}

.logo h1 {
  margin: 0;
  font-size: 1.5rem;
}

.main-nav {
  display: flex;
  gap: 20px;
}

.nav-item {
  text-decoration: none;
  color: var(--text-color);
  font-weight: 500;
  padding: 8px 12px;
  border-radius: 4px;
  transition: all 0.3s;
}

.nav-item:hover, .router-link-active {
  color: var(--primary-color);
  background-color: rgba(64, 158, 255, 0.1);
}

.user-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.login-btn, .register-btn {
  text-decoration: none;
  padding: 8px 16px;
  border-radius: 4px;
  font-weight: 500;
  transition: all 0.3s;
}

.login-btn {
  color: var(--primary-color);
}

.register-btn {
  background-color: var(--primary-color);
  color: white;
}

.login-btn:hover {
  background-color: rgba(64, 158, 255, 0.1);
}

.register-btn:hover {
  background-color: #66b1ff;
}

.user-dropdown {
  cursor: pointer;
  color: var(--text-color);
  display: flex;
  align-items: center;
  gap: 4px;
}
</style>